package com.palm.platform.service;

import com.palm.core.data.Item;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("dictService")
public class CacheDataService {

    private Map<String,List<Item>> dictCache=new HashMap<>();
    //地区列表
    private Map<String,Item>  districts=new HashMap();
    //机构列表
    private  Map<String,Item> baranchs=new HashMap();
    @Db
    SqlToyLazyDao dao;
    @Init
    public void init(){
    	buildDictCache();
//    	buildDistrictCache();
//        buildBranchCache();

    }
    //建立字典缓存
    public void buildDictCache(){
        Map<String,List<Item>> caches=new HashMap();
        List<Map> dicts= dao.findBySql("select type,sn,name from t_dictionary ORDER BY type,rank",new HashMap<>(),Map.class);
        for(Map dictItem:dicts){
            String type=(String)dictItem.get("type");
            List<Item> subCodes= caches.get(type);
            if(subCodes==null){
                subCodes=new ArrayList<>();
                caches.put(type,subCodes);
            }
            subCodes.add(new Item((String)dictItem.get("sn"),(String)dictItem.get("name")));
        }
        dictCache=caches;
    }
    //建立地区缓存
    public void buildDistrictCache(){
    	List<Map> data= dao.findBySql("select code _key,Name title,ParentCode parent,CodeOrder _order from zddistrict",new HashMap<>(),Map.class);
    	Map root=new HashMap();
    	root.put("key", "-");
    	data.add(0, root);
    	districts=buildTree(data);
    }
    public List<Item> getDistrict(String parent){
    	Item item=districts.get(parent);
    	if(item==null||item.getChildren()==null) {
    		return new ArrayList();
    	}
    	return _copy(item.getChildren());
    }
    public List<Item> getBaranchs(String parent){
    	Item item=baranchs.get(parent);
    	if(item==null||item.getChildren()==null) {
    		return new ArrayList();
    	}
    	return _copy(item.getChildren());
    }
    private List<Item> _copy(List<Item> data){
    	return data.stream()
		.map(it->new Item(it.getKey(),it.getTitle()))
		.collect(Collectors.toList());
    }
    //建立机构缓存
    public void buildBranchCache(){
    	List<Map> data= dao.findBySql("select sn _key,name title,psn parent,sort_rank _order from t_branch",new HashMap<>(),Map.class);
    	baranchs=buildTree(data);
    }
    private Map<String,Item> buildTree(List<Map> data){
    	Map<String,Item> map=new HashMap();
    	for(Map m:data) {

    		String title=(String)m.get("title");
    		String key=(String)m.get("key");
    		String parent=(String)m.get("parent");
    		Object order=m.get("order");

    		Item item= new Item(key,title);
    		item.setParent(parent);
    		if(order==null) {
    			item.setOrder(0);
    		}else {
    			item.setOrder(Integer.valueOf(order.toString()));
    		}
    		map.put(key,item);
    	}
    	for(Item item:map.values()) {
    		Item parent=map.get(item.getParent());
    		if(parent!=null) {
    			if(parent.getChildren()==null) {
    				parent.setChildren(new ArrayList());
    			}
    			parent.getChildren().add(item);
    		}
    	}
    	for(Item item:map.values()) {
    		if(item.getChildren()!=null) {
    			Collections.shuffle(item.getChildren());
    		}
    	}
        return map;
    }
    public Map<String,String> getByType(String type){
       List<Item> data= dictCache.get(type);
        Map result= new HashMap<>();
       if(data==null){
           return result;
       }
       for(Item m:data){
           result.put(m.getKey(),m.getTitle());
       }
       return result;
    }
    public Map<String,List<Item>> allCodes(){
        return dictCache;
    }
}
